﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.Models;
using BlackBird.DataAccess;

namespace BlackBird.ControllerHelpers
{
	public class ProjectHelper
	{
		private static int CompareItemListViewDm(ItemInfoDM x, ItemInfoDM y) {
			DateTime dtx = DateTime.Parse(x.CreateDate);
			DateTime dty = DateTime.Parse(y.CreateDate);

			return dtx.CompareTo(dty);
		}
		public static ItemListViewDM GetProjectItemListViewDM(int projectId)
		{
			ItemListViewDM itListView = new ItemListViewDM();

			//Get all Income, expend, Event
			var allevents = EventsDataAccess.GetAllEventsExceptPlanByProjectId(projectId);
			allevents.ForEach(p => itListView.Items.Add(ConvertFromEvent(p)));

			var allbijidans = BiJiaDanDataAccess.GetAllBiJiaDans(projectId.ToString());
            allbijidans.ForEach(p => itListView.Items.Add(ConvertFromBijiadan(p)));

            //付款单
            FuKuanDanDataAccess.AllFuKuanDanDMForCurrentProject(projectId).ForEach(p => itListView.Items.Add(ConvertFromFuKuanDan(p)));

			itListView.Items.Sort(CompareItemListViewDm);

			return itListView;
		}

		private static ItemInfoDM ConvertFromEvent(@event evt)
		{
			ItemInfoDM dmItem = new ItemInfoDM();
			dmItem.Id = evt.idEvent.ToString();
			dmItem.Name = evt.Name;
			dmItem.CreateDate = ((DateTime)(evt.Date)).ToString("yyyy-MM-dd");
			dmItem.CreateOwn = AccountDataAccess.GetUserInfoByUserID(evt.idUser).DisplayName;
			dmItem.type = CommonDataAccess.GetSupportTypesById((int)evt.idEventType);
			dmItem.status = CommonDataAccess.GetSupportTypeStatusById((int)evt.idStatus);

			return dmItem;
		}

        private static ItemInfoDM ConvertFromBijiadan(BiJiaDanDM bijiadanDm)
        {
            ItemInfoDM dmItem = new ItemInfoDM();
            dmItem.Id = bijiadanDm.Id;
            dmItem.Name = bijiadanDm.Name;
            dmItem.CreateDate = ((DateTime)(bijiadanDm.Date)).ToString("yyyy-MM-dd");
            dmItem.CreateOwn = bijiadanDm.CreateUserName;
            dmItem.type = SupportTypes.BiJiaDan;
            dmItem.status = bijiadanDm.Status;

            return dmItem;
        }

        private static ItemInfoDM ConvertFromFuKuanDan(FuKuanDanModel fukuandan)
        {
            ItemInfoDM dmItem = new ItemInfoDM();
            dmItem.Id = fukuandan.Id.ToString();
            dmItem.Name = fukuandan.ProjectName;
            dmItem.CreateDate = Convert.ToDateTime(fukuandan.ShengQingRiQi).ToString("yyyy-MM-dd");
            dmItem.CreateOwn = fukuandan.TianBiaoRen;
            dmItem.type = SupportTypes.FukuanDan;
            dmItem.status = fukuandan.Status;

            return dmItem;
        }

		/// <summary>
		/// Get Income and Expend by ID
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public static IncomeAndExpendInputDM GetIncomeAndExpendById(string id)
		{
			var item = EventsDataAccess.GetEventById(int.Parse(id));
			if (item == null)
				throw new NullReferenceException("Get event by id return null object");

			return ConvertEventToIncomeAndExpend(item);
		}

		/// <summary>
		/// Get Project Event by ID;
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public static ProjectEventDM GetProjectEventById(string id)
		{
			IncomeAndExpendInputDM item = GetIncomeAndExpendById(id);

			return ConvertIncomeAndExpendToEventDM(item);
		}

		private static IncomeAndExpendInputDM ConvertEventToIncomeAndExpend(@event evt)
		{
			var retItem = new IncomeAndExpendInputDM();
			retItem.Id = evt.idEvent.ToString();
			retItem.Name = evt.Name;
			retItem.Count = (float)evt.Count;
			retItem.Date = (DateTime)evt.Date;
			retItem.Description = evt.Comments;
			retItem.type = CommonDataAccess.GetSupportTypesById((int)evt.idEventType);
			retItem.Status = CommonDataAccess.GetSupportTypeStatusById((int)evt.idStatus);

			return retItem;
		}

		private static ProjectEventDM ConvertIncomeAndExpendToEventDM(IncomeAndExpendInputDM ieDM)
		{
			return new ProjectEventDM
			{
				Id = ieDM.Id,
				Name = ieDM.Name,
				Description = ieDM.Description,
				type = ieDM.type
			};
		}

		public static MessagePopupDM GetCurrentUserMessagePopupDM(user currentuser)
		{
			MessagePopupDM mssPopDm = new MessagePopupDM();

            //1)需要处理比价单
			MessageItemDM item = new MessageItemDM();
            item.Title = "您有以下比价单需要处理:";

			var allbijiadans = BiJiaDanDataAccess.GetAllBiJiaDans();
			var allSteps = QuanXianAndProcessHelper.GetUserStepNumber(SupportTypes.BiJiaDan, currentuser);

			var allUserCanProcessBiJiaDan = allbijiadans.Where(v => allSteps.Contains(v.CurrentStepNumber) == true);

			allUserCanProcessBiJiaDan.ToList().ForEach(p => item.Items.Add(ConvertFromBijiadan(p)));

			//有待处理的比价单
			if (item.Items.Count > 0)
			{
				mssPopDm.AllMessageItem.Add(item);
			}

            //2)审核通过的比价单
			MessageItemDM shenheTongGuoItem = new MessageItemDM();
			shenheTongGuoItem.Title = "您有以下比价单审核通过：";

			var shenheTongGuoBiJiaDan = allbijiadans.Where(v => v.Status == SupportTypeStatus.ShenHeWanCheng && v.CreateUser_idUser == currentuser.idUser);
			shenheTongGuoBiJiaDan.ToList().ForEach(d => shenheTongGuoItem.Items.Add(ConvertFromBijiadan(d) ) );

			//处理完成的比价单
			if (shenheTongGuoItem.Items.Count > 0)
			{
				mssPopDm.AllMessageItem.Add(shenheTongGuoItem);
			}

            //3)需要处理付款单
            MessageItemDM fukuandanItem = new MessageItemDM();
            fukuandanItem.Title = "您有以下付款单需要处理:";
            var allSteps_fukuandan = QuanXianAndProcessHelper.GetUserStepNumber(SupportTypes.FukuanDan, currentuser);
            var allfukuandans = FuKuanDanDataAccess.AllFuKuanDanDM();
            var allUserCanProcess_fukuandan = allfukuandans.Where(v => allSteps_fukuandan.Contains(v.CurrentStep) == true);

            allUserCanProcess_fukuandan.ToList().ForEach(d => fukuandanItem.Items.Add(ConvertFromFuKuanDan(d)));

            //处理完成的比价单
            if (fukuandanItem.Items.Count > 0)
            {
                mssPopDm.AllMessageItem.Add(fukuandanItem);
            }

            //4) 通过审核的付款单
            MessageItemDM shenheTongGuo_FukuandanItem = new MessageItemDM();
            shenheTongGuo_FukuandanItem.Title = "您有以下付款单审核通过：";

            var shenheTongGuo_Fukuandans = allfukuandans.Where(v => v.Status == SupportTypeStatus.ShenHeWanCheng && v.CreateUser_idUser == currentuser.idUser);

            shenheTongGuo_Fukuandans.ToList().ForEach(d => shenheTongGuo_FukuandanItem.Items.Add(ConvertFromFuKuanDan(d)));

            //处理完成的比价单
            if (shenheTongGuo_FukuandanItem.Items.Count > 0)
            {
                mssPopDm.AllMessageItem.Add(shenheTongGuo_FukuandanItem);
            }

			return mssPopDm;
		}
	}
}